﻿using FineUI;
using System;
using System.Collections.Generic;
using System.Linq;
using ZHDJ.BranchCenter.BLL;
using ZHDJ.Data.Models.Center;
using ZHDJ.WebPage;

namespace ZHDJ.BranchCenter.Module.PartyRepresentative
{
    public partial class PartyRepresentativeMeetingWithList : PageBase
    {
        #region 属性
        /// <summary>
        /// 是否和个人中心共用
        /// </summary>
        public override bool IsCommonPage
        {
            get
            {
                return true;
            }
        }


        #endregion

        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                // return IsPersonalCenter ? "PartyRepresentativeMeetingWithView_p": "PartyRepresentativeMeetingWithView";
                return "PartyRepresentativeMeetingWithView";
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ControlBusiness.ControlBind(
                ddlStudio, DB.PartyRepresentativeStudios.OrderBy(a => a.CreateTime),
                "StudioName", "ID", true);
                if (!IsSystemAdmin && !CheckPageRight())
                {
                    Response.Clear();
                    Response.Write("未开通党代表工作室，如要开通请联系网站管理员");
                    Response.End();
                }
                BindData();
                LoadData();
            }
        }

        #region 数据加载
        private void BindData()
        {
            // 权限检查
            CheckPowerWithButton("PartyRepresentativeMeetingWithAdd", btnAdd);

            btnAdd.OnClientClick = Dialog.GetShowReference("PartyRepresentativeMeetingWithEdit.aspx?IsPersonalCenter=" + (IsPersonalCenter ? "1" : "0") + "&StudioID=" + ddlStudio.SelectedValue, "新增");

            // 每页记录数
            Grid1.PageSize = ConfigHelper.PageSize;
            ddlGridPageSize.SelectedValue = ConfigHelper.PageSize.ToString();
            if (IsSystemAdmin)
            {
                this.ddlStudio.Readonly = false;
            }
            else
            {
                this.ddlStudio.Readonly = true;
            }
        }
        private void LoadData()
        {
            var ds = PartyRepresentativeMeetingWith.GetGridData(Grid1.PageIndex, Grid1.PageSize, ddlStatus.SelectedValue, txtTitle.Text, ddlStudio.SelectedValue);
            Grid1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            Grid1.DataSource = ds.Tables[0];
            Grid1.DataBind();
        }

        public bool CheckPageRight()
        {
            var rs = true;
            Grid1.EmptyText = "";
            if (!IsPersonalCenter && DB.PartyRepresentativeStudios.Count(a => a.ObjectID == BranchID) < 1)
            {
                rs = false;
            }
            else if (IsPersonalCenter && DB.PartyRepresentativeStudios.Count(a => a.ObjectID == CurrentUser.LoginID) < 1)
            {
                rs = false;
            }

            if (rs)
            {
                var objectid = !IsPersonalCenter ? BranchID : CurrentUser.LoginID;
                var temp = DB.PartyRepresentativeStudios.First(a => a.ObjectID == objectid);
                ddlStudio.SelectedValue = temp.ID;
                return true;
            }
            Grid1.Hidden = true;
            Grid1.EmptyText = "未开通党代表工作室，如要开通请联系网站管理员";
            return false;
        }
        #endregion

        #region 事件
        protected void Grid1_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            CheckPowerWithWindowField("PartyRepresentativeMeetingWithEdit", Grid1, "editField");
            CheckPowerWithLinkButtonField("PartyRepresentativeMeetingWithDelete", Grid1, "deleteField");
        }

        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            LoadData();
        }

        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            LoadData();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string PartyRepresentativeMeetingWithId = GetSelectedDataKeyID(Grid1);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                if (!CheckPower("PartyRepresentativeMeetingWithDelete"))
                {
                    CheckPowerFailWithAlert();
                    return;
                }

                var del = DB.PartyRepresentativeMeetingWiths.FirstOrDefault(t => t.ID == PartyRepresentativeMeetingWithId);
                DB.PartyRepresentativeMeetingWiths.Remove(del);
                DB.SaveChanges();
                LoadData();
            }
        }
        #endregion

        #region 按钮事件
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }
        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            // 在操作之前进行权限检查
            if (!CheckPower("PartyRepresentativeMeetingWithDelete"))
            {
                CheckPowerFailWithAlert();
                return;
            }

            // 从每个选中的行中获取ID（在Grid1中定义的DataKeyNames）
            List<string> ids = GetSelectedDataKeyIDs(Grid1);

            // 执行数据库操作
            DB.PartyRepresentativeMeetingWiths.Where(u => ids.Contains(u.ID)).ToList().ForEach(u => DB.PartyRepresentativeMeetingWiths.Remove(u));
            DB.SaveChanges();

            // 重新绑定表格
            LoadData();
        }
        #endregion

        protected void Dialog_Close(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}